package com.platform.comm.common.batch.processor;

import com.platform.vo.ImportDto;
import com.platform.comm.common.batch.ComBatchExportContext;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.lang.NonNull;

import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * description: 通用数据转换接口
 *
 * @author lch
 */
public class ComExportDataTransformProcessor implements ItemProcessor<Map<String, Object>, ImportDto> {

    private ComBatchExportContext context;
    public ComExportDataTransformProcessor(ComBatchExportContext context) {
        this.context = context;
    }

    @Override
    public ImportDto process(@NonNull Map<String, Object> item) {
        Set<String> enumFieldSet = context.getEnumFieldSet();
        enumFieldSet.forEach((key) -> Optional.ofNullable(item.get(key))
                                              .ifPresent(value -> item.put(key, context.transform(item))));
        ImportDto importDto = new ImportDto();
        importDto.setBucket(item);

        return importDto;
    }
}
